#Region " Imports "

Imports System
Imports System.Data
Imports BlazeApps.Library.Blog
Imports BlazeApps.Library.Options

#End Region

Partial Class Usercontrols_Blog_BlogViewer
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected Property BlogGuid() As Guid
        Get
            Return GetGuidFromVS("BlogID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("BlogID", value)
        End Set
    End Property

    Protected Property PrintMeActive() As Boolean
        Get
            Return GetBooleanFromVS("PrintMeActive")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("PrintMeActive", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        GetQueryStringValues()

        If Me.Page.IsPostBack = False Then
            SetViewed(BlogGuid, False)
            LoadBlog()
            If IsPrintMeOrPopUpActive() = False Then
                LoadOptions()
            End If
        End If

        SetPageState()

    End Sub

#End Region

#Region " Save Data "


#End Region

#Region " Load Data "

    Private Sub LoadOptions()
        Dim Items As OptionDetail = OptionDetail.GetOptions(BlogGuid)

        'Hide/Show Panels based on page settings
        Me.uxFavoritesPanel.Visible = (Items.AllowSubscriptions And EnableFavorites)
        Me.uxRaingPanel.Visible = (Items.AllowRanking And EnableRating)
        Me.uxCommentsPanel.Visible = (Items.AllowComments And EnableComments)
        Me.uxTagsPanel.Visible = (Items.AllowTags And EnableTags)
        Me.uxToolsPanel.Visible = (Items.AllowInfoTools And EnableTools)

        'Enable/Disable Panels based on page settings
        Me.uxFavoritesPanel.Enabled = (Items.AllowSubscriptions And EnableFavorites)
        Me.uxRaingPanel.Enabled = (Items.AllowRanking And EnableRating)
        Me.uxCommentsPanel.Enabled = (Items.AllowComments And EnableComments)
        Me.uxTagsPanel.Enabled = (Items.AllowTags And EnableTags)
        Me.uxToolsPanel.Enabled = (Items.AllowInfoTools And EnableTools)

    End Sub

    Private Sub LoadBlog()
        Dim item As BlogDetail = BlogDetail.GetBlogDetail(BlogGuid)

        Me.uxBlogTitleLabel.Text = item.Title
        Me.uxBlogBodyLiteral.Text = item.Body
        Me.uxCreateDateLable.Text = String.Format("{0}, by {1}", FormatCreateDate(item.Createdate.ToString), String.Format("<a href=""{0}.aspx?pid=cp_profileview&userid={1}"">{1}</a>", AppPage, Membership.GetUser(item.Userid).UserName.ToString))

        SetPageTitle(item.Title)

        If IsUserAuthenticated Then
            If IsAllowedEdit(item.Userid.ToString) Then
                Me.uxEditBlogHyperlink.Visible = True
                Me.uxEditBlogHyperlink.NavigateUrl = String.Format("{0}.aspx?pid=blg_addblog&id={1}", AppPage, BlogGuid)
                Me.uxEditBlogHyperlink.Text = "Edit"
            End If
        End If

        Me.uxFavorites.FavoriteType = BlazeApps.Library.Base.ItemType.Blogs
        Me.uxPageTags.TagType = BlazeApps.Library.Base.ItemType.Blogs
        Me.uxRating.Revision = 1

    End Sub

#End Region

#Region " Form Controls Events "

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        BlogGuid = GetGuidFromQueryString("id")
    End Sub

    Private Sub SetPageState()

        If IsPrintMeOrPopUpActive() = True Then
            Me.uxCommentsList.Visible = False
            Me.uxCommentsList.HideData = True
            Me.uxTools.Visible = False
            Me.uxTools.HideData = True
            Me.uxRating.Visible = False
            Me.uxRating.HideData = True
            Me.uxFavorites.Visible = False
            Me.uxFavorites.HideData = True
            Me.uxPageTags.Visible = False
            Me.uxPageTags.HideData = True
        End If

    End Sub

    Private Function IsAllowedEdit(ByVal UserID As String) As Boolean
        Return (CurrentUserID = ConvertToGuid(UserID) OrElse IsBlogModerator())
    End Function

#End Region

End Class
